/*******************************************************************************
 * Copyright (c) 2008-2009 zhang yuexiang.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package org.xfeep.alaya.orm.jpa.test;


import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;


import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.xfeep.alaya.jta.WeaverService;
import org.xfeep.alaya.orm.jpa.EntityManagerFactoryImp;
import org.xfeep.alaya.orm.jpa.PersistenceUnitScanner;
import org.xfeep.alaya.orm.jpa.SmartEntityManagerImp;
import org.xfeep.asura.bootstrap.Asura;
import org.xfeep.asura.bootstrap.MemoryComponentLoader;
import org.xfeep.asura.bootstrap.XmlComponentLoader;
import org.xfeep.asura.bootstrap.config.XmlMultipleConfigLoader;
import org.xfeep.asura.core.CoreConsts;
import org.xfeep.asura.core.annotation.Ref;
import org.xfeep.asura.core.annotation.Service;
import org.xfeep.asura.core.console.CommandLineService;

public class EntityManagerTestSet {
	
	static Asura asura;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		asura = new Asura();
		asura.start(
				new MemoryComponentLoader(), 
				new MemoryComponentLoader(CommandLineService.class),
				new MemoryComponentLoader(WeaverService.class), 
				new XmlComponentLoader(Thread.currentThread().getContextClassLoader().getResource("META-INF/extern/Atomikos-composite.xml")),
				new MemoryComponentLoader(PersistenceUnitScanner.class, EntityManagerFactoryImp.class, SmartEntityManagerImp.class)
				
		);
		asura.getConfigAdminService().load(new XmlMultipleConfigLoader("jpaTestConfig.xml"));
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		asura.shutdown();
	}
	
	
	@Service
	@TransactionManagement(TransactionManagementType.CONTAINER)
	public static class EntityManagerTestClient {
		
		@Ref(matcher="_cid=jpa:testjpa"+CoreConsts.ON_DEMAND_SEPRATOR)
		EntityManager em;
		
		public void insert(Person p){
			em.persist(p);
		}
		
		
		public Person find(String id){
			return em.find(Person.class, id);
		}
		
		
		public Person update(Person p){
			p = em.merge(p);
//			em.flush();
			return p;
		}
		
		public void updateWithException(Person p) {
			em.merge(p);
			throw new RuntimeException("test update with exception");
		}
		
		public void remove(Person p){
			em.remove(p);
		}
		
	}
	
	@Test
	public  void testEntityManagerClient() throws ParseException {
		asura.getComponentManager().add(EntityManagerTestClient.class);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		EntityManagerFactoryImp emf = asura.getApplicationServiceSpace().findService(EntityManagerFactoryImp.class);
		assertNotNull(emf);
		assertNotNull(emf.getPersistenceUnitInfo().getJtaDataSource());
		assertEquals(emf.getPersistenceUnitInfo().jtaDataSourceName, "testjpaDs");
		EntityManagerTestClient etc = asura.getApplicationServiceSpace().findService(EntityManagerTestClient.class);
		assertNotNull(etc);
		Person zs = new Person("zs", "张三", df.parse("2001-03-23"), "zs@xfeep.org");
		etc.insert(zs);
		
		Person fzs = etc.find("zs");
		assertEquals(zs.id, fzs.id);
		
		try{
			fzs.email = "zsfailupdate@xfeep.org";
			etc.updateWithException(fzs);
			fail("excepted exception did not happen");
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		fzs = etc.find("zs");
		assertEquals(zs.email, fzs.email);
		
		fzs.email = "zsupdate@xfeep.org";
		etc.update(fzs);
		zs = etc.find("zs");
		assertEquals(fzs.email, zs.email);
		
	}

	@Before
	public void setUp() throws Exception {
		
	}

	
	
	
	@After
	public void tearDown() throws Exception {
		
	}

}
